 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import View.Model.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;

/**
 *
 * @author ANA
 */
public class MainPage extends javax.swing.JFrame {

    /**
     * Creates new form MainPage
     */
    public Project project = new Project();
    public Topic topic = new Topic();
    public MainPage() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        arvoreCoisa = new javax.swing.JTree();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();

        openMenuItem = new javax.swing.JMenuItem();
        openProjectMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        serverMenuItem = new javax.swing.JMenuItem();
        topicMenuItem = new javax.swing.JMenuItem();
        protocolPage = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        projectMenu = new javax.swing.JMenu();
        componentMenuItem = new javax.swing.JMenuItem();
        portMenuItem = new javax.swing.JMenuItem();
        controllerMenuItem = new javax.swing.JMenuItem();
        thingMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("<<Project>> SmartAgreeculture");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("<<Server>>Web");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("<<Protocol>>MQTT");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("<<Thing>> SmartThing");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<<Controller>> Esp32");
        javax.swing.tree.DefaultMutableTreeNode treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("PortaA");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("PortaB");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("PortaC");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("PortaD");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<<Component> Motor");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Porta1");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<<Component> SensorLuz");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Porta1");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<<Component>>SensorTermostato");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Porta1");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("<<Component>>SensorHumidade");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Porta1");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        arvoreCoisa.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(arvoreCoisa);

        desktopPane.add(jScrollPane1);
        jScrollPane1.setBounds(0, 0, 210, 350);

        fileMenu.setMnemonic('f');
        fileMenu.setText("File");
        fileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        openMenuItem.setMnemonic('o');
        openMenuItem.setText("New Project");
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openMenuItem);

        openProjectMenuItem.setText("Open Project");
        openProjectMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openProjectMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openProjectMenuItem);

        saveAsMenuItem.setMnemonic('a');
        saveAsMenuItem.setText("Save");
        saveAsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAsMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveAsMenuItem);


        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        projectMenu.setMnemonic('e');
        projectMenu.setText("Project");

        serverMenuItem.setMnemonic('s');
        serverMenuItem.setText("Server");
        serverMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                serverMenuItemActionPerformed(evt);
            }
        });
        projectMenu.add(serverMenuItem);

        protocolPage.setText("Protocol");
        protocolPage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                protocolPageActionPerformed(evt);
            }
        });
        projectMenu.add(protocolPage);

        topicMenuItem.setText("Topic");
        topicMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                topicMenuItemActionPerformed(evt);
            }
        });
        projectMenu.add(topicMenuItem);

        componentMenuItem.setMnemonic('c');
        componentMenuItem.setText("Component");
        componentMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                componentMenuItemActionPerformed(evt);
            }
        });
        projectMenu.add(componentMenuItem);

        portMenuItem.setMnemonic('p');
        portMenuItem.setText("Port");
        portMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                portMenuItemActionPerformed(evt);
            }
        });
        projectMenu.add(portMenuItem);

        controllerMenuItem.setMnemonic('c');
        controllerMenuItem.setText("Controller");
        controllerMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                controllerMenuItemActionPerformed(evt);
            }
        });
        projectMenu.add(controllerMenuItem);

        thingMenuItem.setMnemonic('t');
        thingMenuItem.setText("Thing");
        thingMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               thingMenuItemActionPerformed(evt);
            }
        });
        projectMenu.add(thingMenuItem);
        menuBar.add(projectMenu);

        helpMenu.setMnemonic('h');
        helpMenu.setText("Help");

        contentMenuItem.setMnemonic('c');
        contentMenuItem.setText("Contents");
        helpMenu.add(contentMenuItem);

        aboutMenuItem.setMnemonic('a');
        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(desktopPane, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fileMenuActionPerformed

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
        // TODO add your handling code here:
        NewProject tela = new NewProject(arvoreCoisa);
        tela.setVisible(true);
    }//GEN-LAST:event_openMenuItemActionPerformed

    private void serverMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverMenuItemActionPerformed
        // TODO add your handling code here:
        ServerPage tela = new ServerPage(project);
        tela.setVisible(true);
    }//GEN-LAST:event_serverMenuItemActionPerformed

    private void componentMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverMenuItemActionPerformed
        // TODO add your handling code here:
        ComponentPage tela = new ComponentPage();
        tela.setVisible(true);
    }//GEN-LAST:event_serverMenuItemActionPerformed

    private void portMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverMenuItemActionPerformed
        // TODO add your handling code here:
        PortPage tela = new PortPage();
        tela.setVisible(true);
    }//GEN-LAST:event_serverMenuItemActionPerformed

    private void controllerMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverMenuItemActionPerformed
        // TODO add your handling code here:
        ControllerPage tela = new ControllerPage();
        tela.setVisible(true);
    }//GEN-LAST:event_serverMenuItemActionPerformed

    
    private void thingMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverMenuItemActionPerformed
        // TODO add your handling code here:
        ThingPage tela = new ThingPage();
        tela.setVisible(true);
    }//GEN-LAST:even

    private void openProjectMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openProjectMenuItemActionPerformed
        // TODO add your handling code here:
        OpenProject tela = new OpenProject(project, topic);
        tela.setVisible(true);
        /*String aux = project.getProjectFolder() + "project.json";
        String conteudo = "";
        try {
            conteudo = new String(Files.readAllBytes(Paths.get(aux)));
        } catch (IOException ex) {
            Logger.getLogger(ServerPage.class.getName()).log(Level.SEVERE, null, ex);
        }
        JSONObject objetoJson = new JSONObject(conteudo);
        
   
        //JSONObject objetoJson = jsonArray.getJSONObject(x);
            
        project.setNomeServer(objetoJson.getString("nomeServer"));
        project.setPasswordServer(objetoJson.getString("passwordServer"));
        project.setIdServer(objetoJson.getString("idServer"));
        project.setPortaServer(objetoJson.getString("portaServer"));*/
        
    }//GEN-LAST:event_openProjectMenuItemActionPerformed

    private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
        // TODO add your handling code here:
        String aux = project.getProjectFolder() + File.separator + project.getProjectNome() + File.separator + "project.json";
       
        
        /*System.out.println(project.getProjectNome());
        System.out.println(aux);
        System.out.println(project.getProjectFolder());*/
        
        
        //String aux = project.getProjectFolder() + "project.json";
        //JSONArray jsonArray = new JSONArray();
        
        //for(int i=0;i < project.size(); i++){
        JSONObject objetoJson = new JSONObject();
        
        objetoJson.put("projectNome", project.getProjectNome());
        objetoJson.put("projectFolder", project.getProjectFolder());
        objetoJson.put("nomeServer", project.getNomeServer());
        objetoJson.put("passwordServer", project.getPasswordServer());
        objetoJson.put("idServer", project.getIdServer());
        objetoJson.put("portaServer", project.getPortaServer());
        objetoJson.put("nomeProt", project.getNomeProt());
        objetoJson.put("userProt", project.getUserProt());
        objetoJson.put("passwordProt", project.getPasswordProt());
    
        //JSONObject jsonObject = new JSONObject();
        //jsonObject.put("objects", jsonArray);
            
        FileWriter writeFile;
        try {
            writeFile = new FileWriter(aux);
            writeFile.write(objetoJson.toString());
            writeFile.close();
        } catch (IOException ex) {
            Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        JSONObject objetoJson2 = new JSONObject();
        
        objetoJson2.put("name", topic.getName());
        objetoJson2.put("type", topic.getType());
        
        aux = project.getProjectFolder() + File.separator + project.getProjectNome() + File.separator + "topic.json";

        try{
            writeFile = new FileWriter(aux);
            writeFile.write(objetoJson2.toString());
            writeFile.close();
        } catch (IOException ex) {
            Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }//GEN-LAST:event_saveAsMenuItemActionPerformed

    private void protocolPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_protocolPageActionPerformed
        // TODO add your handling code here:
        ProtocolPage tela = new ProtocolPage(project);
        tela.setVisible(true);
    }//GEN-LAST:event_protocolPageActionPerformed

    private void topicMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topicMenuItemActionPerformed
        // TODO add your handling code here:
        TopicPage tela = new TopicPage(topic);
        tela.setVisible(true);
    }//GEN-LAST:event_topicMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainPage().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JTree arvoreCoisa;
    private javax.swing.JMenuItem contentMenuItem;
    private javax.swing.JMenuItem componentMenuItem;
    private javax.swing.JMenuItem portMenuItem;
    private javax.swing.JMenuItem controllerMenuItem;
    private javax.swing.JMenuItem thingMenuItem;
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenu projectMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuItem topicMenuItem;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem openProjectMenuItem;
    private javax.swing.JMenuItem protocolPage;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem serverMenuItem;
    // End of variables declaration//GEN-END:variables

}
